//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Diagram.Serialization;

namespace OpenLS.Drawing.Diagram
{
    class DiagramContainer: IOfficeXmlSerializable
    {
//        private DataModel _data;
        private DataModel _dataModel;
        private DiagramDefinition _definition;
        private StyleDefinition _styleDefinition;
        private ColorTransform _colorTransform;

        /// <summary>
        /// Generates an object from its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read(DiagramConstants.DiagramNamespace + "relIds"))
            {
                _dataModel = readRelatedElement<DataModel>(c, "dm");
                _definition = readRelatedElement<DiagramDefinition>(c, "lo");
                _styleDefinition = readRelatedElement<StyleDefinition>(c, "qs");
                _colorTransform = readRelatedElement<OpenLS.Drawing.Diagram.ColorTransform>(c, "cs");
            }
        }

        private static T readRelatedElement<T>(ReadContext c, string relationshipName) where T : IOfficeXmlSerializable, new()
        {
            string dm = c.GetString(OfficeConstants.RelationshipNamespace + relationshipName);
            using (var dmContext = c.OpenRelatedContext(dm, c.NamespaceUri))
            {
                return dmContext.ReadElement<T>();
            }
        }

        /// <summary>
        /// Convert an object into its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write(DiagramConstants.DiagramNamespace + "relIds"))
            {
                c.DeclareNamespace(Drawing.Serialization.DrawingConstants.DrawingNamespace);
                writeRelatedElement<DataModel>(c, "dm", _dataModel, 
                    "../diagrams/data{0}.xml", DiagramConstants.diagramDataRelationship, DiagramConstants.diagramDataContentType);
                writeRelatedElement<DiagramDefinition>(c, "lo", _definition, "../diagrams/layout{0}.xml", DiagramConstants.diagramLayoutRelationship,
                    DiagramConstants.diagramLayoutContentType);
                writeRelatedElement<StyleDefinition>(c, "qs", _styleDefinition, "../diagrams/quickStyle{0}.xml",
                    DiagramConstants.diagramQuickStylesRelationship, DiagramConstants.diagramStyleContentType);
                writeRelatedElement<ColorTransform>(c, "cs", _colorTransform,
                    "../diagrams/colors{0}.xml", DiagramConstants.diagramColorsRelationship, DiagramConstants.diagramColorsContentType);
            }
        }

        private void writeRelatedElement<T>(WriteContext c, string relName, IOfficeXmlSerializable obj, string template,
            string relationName, string contentType)
        {
            using (var relatedContext = c.CreateRelatedContext(DiagramConstants.DiagramNamespace, c.GetAvailableUri(template), relationName, contentType, false))
            {
                c.SetString(OfficeConstants.RelationshipNamespace +relName, relatedContext.Relationship.Id);
                relatedContext.WriteElement(obj);
            }
        }
    }
}